package com.ihr360.cnb.module.insurance.controller;

import com.google.common.collect.Lists;
import com.ihr360.cnb.module.insurance.model.excel.HeaderConstants;
import com.ihr360.cnb.module.insurance.service.StaffBenefitImportService;
import com.ihr360.commons.context.session.Ihr360Session;
import com.ihr360.commons.context.session.Ihr360SessionContextHolder;
import com.ihr360.excel.specification.ColumnSpecification;
import com.ihr360.excel.specification.CommonSpecification;
import com.ihr360.excel.validation.SpreadSheetValidator;
import com.ihr360.excel.validation.annonation.SpreadSheetValid;
import com.ihr360.excel.vo.CustomSpreadSheetValidateResult;
import com.ihr360.excel.vo.SpreadSheetSpecification;
import com.ihr360.excel.vo.SpreadSheetValidatorRequest;
import com.ihr360.validation.ValidationUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Stone.Shi
 * @description
 * @date 2018-05-16 10:31:39.
 */
@Component
@SpreadSheetValid(code = "BenefitStaff")
public class StaffBenefitImportValidator implements SpreadSheetValidator {

    private static final String PARAM_CATEGORY = "category";

    @Autowired
    private StaffBenefitImportService staffBenefitImportService;

    @Override
    public CustomSpreadSheetValidateResult customValidate(SpreadSheetValidatorRequest request) {

        Ihr360Session session = Ihr360SessionContextHolder.getSession();
        if (session == null || session.getUserInfo() == null
                || session.getUserInfo().getCompanyId() == null) {
            ValidationUtils.reject(null, "【方案员工关联校验】请求参数无效");
        }

        return staffBenefitImportService.validateExcelData(session.getUserInfo().getCompanyId(), request);
    }

    @Override
    public SpreadSheetSpecification commonSpecification(SpreadSheetValidatorRequest request) {
        SpreadSheetSpecification spreadSheetSpecification = new SpreadSheetSpecification();
        CommonSpecification commonSpecification = CommonSpecification.createCommonSpecification(true);

        List<List<String>> requiredHeaders = Lists.newArrayList();
        ArrayList<String> staffNameColumn = Lists.newArrayList(HeaderConstants.NAME);
        requiredHeaders.add(staffNameColumn);
        ArrayList<String> mobileNoColumn = Lists.newArrayList(HeaderConstants.MOBILE_NO);
        requiredHeaders.add(mobileNoColumn);

        commonSpecification.setHeaderColumnJudge(requiredHeaders);
        commonSpecification.setStaffNameAlias(staffNameColumn);
        commonSpecification.setMobileNoAlias(mobileNoColumn);
        spreadSheetSpecification.setCommonSpecification(commonSpecification);

        List<ColumnSpecification> columnSpecifications = Lists.newArrayList();
        ColumnSpecification dateSpecification = ColumnSpecification.createCellSpecification(Date.class, true);
        dateSpecification.addColumns(HeaderConstants.SI_PAY_START);
        dateSpecification.addColumns(HeaderConstants.SI_PAY_END);
        dateSpecification.addColumns(HeaderConstants.HF_PAY_START);
        dateSpecification.addColumns(HeaderConstants.HF_PAY_END);
        columnSpecifications.add(dateSpecification);

        ColumnSpecification doubleSpecification = ColumnSpecification.createCellSpecification(Double.class, true);
        doubleSpecification.addColumns(HeaderConstants.SI_BASE);
        doubleSpecification.addColumns(HeaderConstants.HF_BASE);
        columnSpecifications.add(doubleSpecification);

        spreadSheetSpecification.setColumnSpecifications(columnSpecifications);

        return spreadSheetSpecification;
    }
}
